package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.aliyuncs.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.DishFlavor;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    /**
     * 分页查询
     * @param pageNum
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page<Dish> pageList(Integer pageNum, Integer pageSize, String name) {
        //创建分页查询对象
        Page<Dish> page = new Page<>(pageNum,pageSize);
        //创建条件对象
        LambdaQueryWrapper<Dish> wrapper= new LambdaQueryWrapper<>();
        //设置条件
        wrapper.like(name!=null,Dish::getName,name);
        //分页查询
        page = dishMapper.selectPage(page, wrapper);
        //获取结果集
        List<Dish> dishList = page.getRecords();
        //遍历结果集
        for (Dish dish : dishList) {
            //使用菜品中的category_id，根据id查询分类对象
            Category category = categoryMapper.selectById(dish.getCategoryId());
            //将菜品分类名称赋值给
            dish.setCategoryName(category.getName());

            //创建菜品口味查询条件
            LambdaQueryWrapper<DishFlavor> wrapper1= new LambdaQueryWrapper<>();
            //设置查询条件为菜品的口味id
            wrapper1.eq(DishFlavor::getDishId,dish.getId());
            //使用dishFlavorMapper查询口味
            List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(wrapper1);
            //将查询到的口味赋值给菜品
            dish.setFlavors(dishFlavors);
        }
        return page;
    }

    /**
     * 添加菜品
     * @param dish
     */
    @Override
    @Transactional
    public void dishSave(Dish dish) {
        //mabatisplus提供的雪花算法id的获取
        long id = IdWorker.getId();
        dish.setId(id);
        dish.setCode(String.valueOf(id));
        dish.setStatus(1);
        dish.setSort(0);
        dishMapper.insert(dish);

        List<DishFlavor> flavors = dish.getFlavors();
        //判断菜品口味表是否为空
        if (CollectionUtil.isNotEmpty(flavors)){
            //遍历集合
            for (DishFlavor flavor : flavors) {
                //将口味信息id加入口味表中
                flavor.setDishId(dish.getId());
                //添加口味
                dishFlavorMapper.insert(flavor);
            }
        }
    }

    /**
     * 根据id查询菜品
     * @param id
     * @return
     */
    @Override
    public Dish findById(Long id) {
        Dish dish = dishMapper.selectById(id);
        //创建口味查询条件
        LambdaQueryWrapper<DishFlavor> wrapper= new LambdaQueryWrapper<>();
        wrapper.eq(id!=null,DishFlavor::getDishId,dish.getId());
        //查询口味
        List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(wrapper);
        //将查询到的口味赋值给菜品口味
        dish.setFlavors(dishFlavors);
        return dish;
    }

    /**
     * 菜品的更新
     * @param dish
     */
    @Override
    public void update(Dish dish) {

        dishMapper.updateById(dish);
        //创建条件对象
        LambdaQueryWrapper<DishFlavor> wrapper= new LambdaQueryWrapper<>();
        //设置条件
        wrapper.eq(DishFlavor ::getDishId,dish.getId());
        //删除口味信息
        dishFlavorMapper.delete(wrapper);
        //获取修改的菜品口味信息
        List<DishFlavor> dishFlavors = dish.getFlavors();
        //判断口味是否为空
        if (CollectionUtil.isNotEmpty(dishFlavors)){
            //遍历口味信息
            for (DishFlavor dishFlavor : dishFlavors) {
                //将菜品id设置给口味的菜品id
                dishFlavor.setDishId(dish.getId());
                //将菜品口味添加到口味表中
                dishFlavorMapper.insert(dishFlavor);
            }
        }


    }

    /**
     * 菜品的批量删除
     * @param ids
     * @return
     */
    @Override
    public String deletes(Long[] ids) {
        String messAge="";
        //遍历要删除的菜品id
        for (Long id : ids) {
            //通过id获取菜品
            Dish dish = dishMapper.selectById(id);
            //通过菜品获取销售状态
            Integer status = dish.getStatus();
            //判断销售状态
            if (status==1){
                //不让删除,抛出异常
                messAge+=dish.getName()+",";
                continue;
            }

            //创建口味查询条件对象
            LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(DishFlavor::getDishId,id);
            //删除口味表中的口味信息
            dishFlavorMapper.delete(wrapper);
            //删除菜品信息
            dishMapper.deleteById(id);
        }
        return messAge;
    }

    /**
     * 修改销售状态
     * @param status
     * @param ids
     */
    @Override
    public void statusUpdate(Integer status, Long[] ids) {
        //遍历批量修改的id
        for (Long id : ids) {
            //获取菜品对象
            Dish dish = dishMapper.selectById(id);
            //设置销售状态
            dish.setStatus(status);
            //更新菜品
            dishMapper.updateById(dish);
        }
    }

    /**
     * 套餐的菜品信息回显
     * @param categoryId
     * @param name
     * @return
     */
    @Override
    public List<Dish> findByIdNamelike(Long categoryId, String name) {

        LambdaQueryWrapper<Dish> wrapper= new LambdaQueryWrapper<>();
        wrapper.eq(categoryId!=null,Dish::getCategoryId,categoryId);
        wrapper.like(StringUtils.isNotEmpty(name),Dish::getName,name);
        //为套餐准备菜品,所以停售的菜品不能出现
        wrapper.eq(Dish::getStatus,1);
        List<Dish> dishList = dishMapper.selectList(wrapper);

        //遍历菜品,获取菜品口味
        if (CollectionUtil.isNotEmpty(dishList)){
            for (Dish dish : dishList) {
                LambdaQueryWrapper<DishFlavor> wrapper1= new LambdaQueryWrapper<>();
                wrapper1.eq(DishFlavor::getDishId,dish.getId());
                List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(wrapper1);
                dish.setFlavors(dishFlavors);
            }
        }
        return dishList;
    }


}
